<?php
namespace app\home\controller;

use app\home\logic\GoodsLogic;
use app\home\logic\CartLogic;



class Goods extends Base {
    private $user = false;
    public $cartLogic; // 购物车逻辑操作类

    public function _initialize(){
        parent::_initialize();
        $this->cartLogic = new CartLogic();
        $this->user = $this->user();
    }

    //球位详情
    public function detail(){
        if (!$this->user) $this->redirect('User/login');
        $goods_id = I('param.itemid');
        $detail = M('goods')->where(array('goods_id' => $goods_id))->find();
        $detail['image'] = unserialize($detail['image']);
        $this->assign('title','球位详情');
        $this->assign('detail',$detail);
        return $this->fetch();
    }
    //产品中心首页
    public function index(){

        if (!$this->user) $this->redirect('User/login',array('type' => 2));
        //banner轮播
        $banner = M('ad')->where(array('pid' => 1,'enabled' => 1))->order('orderby')->limit(3)->select();
        $this->assign('banner',$banner);
        //产品分类
        $category = M('goods_category')->where(array('parent_id' => 2))->select();
        $this->assign('category',$category);
        //精品球具
        $list_1 = M('goods')->where(array('cat_id' => 5,'is_on_sale' => 1))->order('sort')->limit(4)->select();
        $this->assign('list_1',$list_1);
        //精品球
        $list_2 = M('goods')->where(array('cat_id' => 3,'is_on_sale' => 1))->order('sort')->limit(4)->select();
        $this->assign('list_2',$list_2);
        $this->assign('user',$this->user);
        $this->assign('title','产品中心');
        return $this->fetch();
    }


    //产品列表
    public function goodsList(){
        $cat_id = I('param.cat_id');
        $this->assign('cat_id',$cat_id);
        //产品分类
        $category = M('goods_category')->where(array('parent_id' => 2))->select();
        $this->assign('category',$category);

        $this->assign('title','产品列表');
        return $this->fetch('goodsList');
    }

    public function ajaxGoodsList(){
        $cat_id = I('param.cat_id');
        $list = M('goods')->where(array('cat_id' => $cat_id,'is_on_sale' => 1))->order('sort')->select();
        $this->assign('list',$list);
        return $this->fetch('ajaxGoodsList');
    }

    //产品详情
    public function goods_detail(){
        if (!$this->user) $this->redirect('User/login',array('type' => 2));
        $goods_id = I('param.item_id');
        $goodsInfo = M('goods')->where(array('goods_id' => $goods_id))->find();
        $goodsInfo['image'] = unserialize($goodsInfo['image']);
        $goodsLogic = new GoodsLogic();
        $goodsInfo['spec'] = $goodsLogic->get_spec($goodsInfo['goods_id']);
        $spec_goods_price = M('spec_goods_price')->where("goods_id", $goodsInfo['goods_id'])->getField("key,price,store_count,vip_price");
        $goodsInfo['spec_goods_price'] = json_encode($spec_goods_price,true);
        $evaluate = M('order_goods')
            ->alias('og')
            ->Join('shop_order so','og.order_id = so.order_id','LEFT')
            ->Join('user u','u.user_id = so.user_id','LEFT')
            ->where([
               'goods_id'=>$goods_id,
                'order_status'=>3
            ])
            ->field([
                'u.head_img',
                'u.username',
                'so.level',
                'so.content',
                'so.evaluation_time',
            ])
            ->group(
                'so.order_id,
                so.user_id'
            )
            ->order([
                'so.order_id'
            ])
            ->select();
            //pr($evaluate);
        $this->assign('evaluate',$evaluate);
        $this->assign('goodsInfo',$goodsInfo);
        $this->assign('user',$this->user);
        $this->assign('vip',strtotime($this->user['vip_time']) >= time() && $this->user['vip'] ? 1 :0);
        $this->assign('title','产品详情');
        return $this->fetch();
    }

    //点餐首页
    public function order_index(){
    	//die;
        //点餐分类
        $category = M('goods_category')->where(array('parent_id' => 7))->select();
        foreach ($category as $key=>$val){
            $category[$key]['goods'] = M('goods')->field('goods_id,goods_name,goods_remark,original_img,shop_price,original_price')->where(array('cat_id' => $val['id'],'is_on_sale' => 1))->order('sort')->select();
            $category[$key]['count'] = M('meal_cart')->where(array('cat_id' => $val['id'],'user_id' => $this->user['user_id']))->count();
        }
        $this->assign('category',$category);
        $this->assign('category2',$category);
        //dump($category);die;

        //合计
        $result = $this->cartLogic->getUserCartList2(1);
        if(empty($result['total_price'])){
            $result['total_price'] = array( 'total_fee' =>0, 'num' => 0);
        }
        $this->assign('total_price', $result['total_price']); // 总计

        //直接显示购物车
        $this->assign('is_show',I('param.is_show'));
        $this->assign('title','点餐');
        return $this->fetch();
    }

    //点餐商品详情
    public function order_detail(){
        $goods_id = I('param.goods_id');
        $goodsInfo = M('goods')->where(array('goods_id' => $goods_id))->find();
        $goodsLogic = new GoodsLogic();
        $goodsInfo['spec'] = $goodsLogic->get_spec($goodsInfo['goods_id']);
        $spec_goods_price = M('spec_goods_price')->where("goods_id", $goodsInfo['goods_id'])->getField("key,price,store_count");
        $goodsInfo['spec_goods_price'] = json_encode($spec_goods_price,true);
        $this->assign('goodsInfo',$goodsInfo);
        return $this->fetch();
    }


}